Imports IICModel.BusObjects.Mappers
Imports IICModel.ConversionObjects.Mappers
Imports IICModel.BusObjects
Imports IICModel.ConversionObjects

Namespace DataConbversionLogic
    Public Class PXSClassMember
        Inherits BaseDataConverter

        Protected Overrides Sub mapData(ByVal source As org.model.lib.Model.IModelObject, _
                                        ByRef destination As org.model.lib.Model.IModelObject)

            Dim s As PXS_PAR = CType(source, PXS_PAR)
            Dim d As ClassMember = CType(destination, ClassMember)

            'we must find a CFA class from the period code
            Dim c As Period = PeriodDataUtils.findOne("diplomasupertypeid=4 and PeriodCode={0}", s.ExamPeriod)
            If c Is Nothing Then
                Me.logMsg(s.Id & " skipped, Period not found from code " & s.ExamPeriod)
                destination = Nothing
                Exit Sub
            End If

            Dim st As Student = StudentDataUtils.findOne("MemberNumber={0}", s.MemberNo)
            If st Is Nothing Then
                Me.logMsg(s.Id & " skipped, Student not found from MemberNumber " & s.MemberNo)
                destination = Nothing
                Exit Sub
            End If
            If c.Classes.Count = 0 Then
                Dim pprddb As New PXS_PRDSDBMapper
                pprddb.dbConn = Me.dataConvMapper.dbConn
                Dim pprd As PXS_PRDS = pprddb.findWhere("Code={0}", s.ExamPeriod)

                'for CFA, we just add the class! There is always one class
                Dim p As PeriodClass = New PeriodClass
                p.PeriodId = c.PeriodId
                p.LessonId = LessonDataUtils.findOne("lessoncode='CFA'").LessonId
                p.PassGrade = CType(pprd.PassGrade, Integer?)
                p.DataConversionSourceTable = getTableName()

                PeriodClassDataUtils.savePeriodClass(p)

                c.AddClass(p)

                Me.logMsg(s.Id & " Added new class to Period: " & c.PeriodName)

            End If

            If Me.newdbMapper.findWhere("StudentId={0} and ClassId={1}", _
                                            st.Id, c.Classes(0).ClassId) IsNot Nothing Then
                Me.logMsg(s.Id & " skipped, wull result in Duplicate classmember ")
                destination = Nothing
                Exit Sub
            End If

            d.StudentId = st.Id
            d.CandidateCode = s.CandNo
            d.ClassId = c.Classes(0).ClassId
            d.DataConversionSourceTable = getTableName()
            If s.GRADE = 0 Then
                d.Attend = False
                d.Grade = Nothing
            Else
                d.Grade = CType(s.GRADE, Integer?)
                d.Attend = True
            End If


            'the we set it so that we can save later
            d.PeriodClass.Period.IsOpen = True

        End Sub

        Protected Overrides Function getTableName() As String
            Return "PXS_PAR"
        End Function

        Protected Overrides Sub instantiateMappers()

            Me.dataConvMapper = New PXS_PARDBMapper()
            Me.newdbMapper = New ClassMemberDBMapper

        End Sub

        Protected Overrides Function checkData(ByVal source As IModelObject, _
                                  ByVal destination As IModelObject) As Boolean

            Return True

        End Function


    End Class
End Namespace